package
{
	import caurina.transitions.Tweener;
	
	import display.Align;
	
	import events.SkinFactoryEvent;
	
	import flash.events.NativeWindowBoundsEvent;
	
	import kr.pfg.pfgTracer.model.ReceiveManager;
	import kr.pfg.pfgTracer.model.event.ReceiveMessageEvent;
	import kr.pfg.pfgTracer.model.event.ReceiveSwfInfoEvent;
	import kr.pfg.pfgTracer.model.event.VListItemSelectEvent;
	import kr.pfg.pfgTracer.model.vo.EchoClient;
	import kr.pfg.pfgTracer.view.BaseWindow;
	import kr.pfg.pfgTracer.view.controller.VController;
	import kr.pfg.pfgTracer.view.event.VListEvent;
	import kr.pfg.pfgTracer.view.list.VListWindow;
	import kr.pfg.pfgTracer.view.statusbar.VStatusBar;
	import kr.pfg.pfgTracer.view.workspace.VWorkSpaceManager;
	
	import skinfactory.SkinFactory;
	import skinfactory.SkinImage;
	
	public class PFGTracerMain extends BaseWindow
	{
		private var skinFactory: SkinFactory;
		
		private var logo: SkinImage;
		
		private var component_Workspace		: VWorkSpaceManager;
		private var component_StatusBar		: VStatusBar;
		private var component_Controller	: VController;
		
		// add Hazbola / Tracer List Viewer
		private var component_List			: VListWindow;
		
		// add Hazbola
		private var receiveManager: ReceiveManager;
		
		
		public function PFGTracerMain()
		{
			this.projectName = " PFGTracer " ;
			
			super();
			
			this.skinDataLoad();	
		}
		private function skinDataLoad(): void
		{
			skinFactory = SkinFactory.getInstance();
			skinFactory.loadSkinXMLData( "skin/skinData.xml" );
			skinFactory.loadSkinImageData( "skin/skin.png" );
			skinFactory.addEventListener( SkinFactoryEvent.LOAD_COMPLETE, onSkinDataLoadComplete );
		}
		private function onSkinDataLoadComplete( e: SkinFactoryEvent ): void
		{
			this.init();
		}
		override protected function init(): void
		{
			trace("■ INIT ▧▧▧▧▧▧▧▧▧▧▧▧");
			super.init();
			
			stage.nativeWindow.addEventListener( NativeWindowBoundsEvent.RESIZING, onResizeingEventListener );
			stage.nativeWindow.addEventListener( NativeWindowBoundsEvent.RESIZE, onResizeingEventListener );
			
			logo = new SkinImage( "window_title" );
			logo.mouseEnabled = false;
			
			// StatusBar 
			component_StatusBar = new VStatusBar();
			component_StatusBar.width = bg.width;
			component_StatusBar.height = 27;
			
			// StatusBar Message 입력 함수
			component_StatusBar.txtMsg = "PFG Tracer Ready!";
			component_StatusBar.txtMemory = "100";
						
			
			// List Viwer
			component_List = new VListWindow();
			component_List.height = stage.stageHeight - dragArea.height - component_StatusBar.height - 8;
			component_List.width = 8;
			component_List.addEventListener( VListEvent.SHOW_OFF, onListShowOffEventListener );
			component_List.addEventListener( VListEvent.SHOW_ON, onListShowOnEventListener );
			component_List.addEventListener( VListItemSelectEvent.VLIST_ITEM_CLICK, onListItemClickListener );
			
			// Workspace
			component_Workspace = new VWorkSpaceManager();
			component_Workspace.width = stage.stageWidth - component_List.width - 10;
			component_Workspace.height = stage.stageHeight - dragArea.height - component_StatusBar.height - 8;
			
			
			// Receive Manager
			receiveManager = new ReceiveManager();
			receiveManager.addEventListener( ReceiveSwfInfoEvent.DATA_SWFINFORMATION, informationHandler );
			receiveManager.addEventListener( ReceiveMessageEvent.DATA_RECEIVED, receivedHandler );
			receiveManager.addEventListener( ReceiveMessageEvent.DATA_ERROR, receivedErrorHandler );
			
			this.setPosition();
			this.initChild();
		}
		
		/**▧▧▧ About RecevieManager ▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧**/
		
		private function informationHandler ( e: ReceiveSwfInfoEvent ): void
		{
			trace (  "[ e.data ] " + e.data  )
			component_List.addItem( e.data as EchoClient );			
			component_Workspace.addWorkspace( e.data as EchoClient  );
			component_StatusBar.txtMsg = e.data.swfMember[0];
		}
				
		private function receivedErrorHandler ( e: ReceiveMessageEvent ): void
		{
			//component_Workspace.setData ( "에러가 발생하였습니다 ", e.receiveType, 0 );
		}
		
		private function receivedHandler ( e: ReceiveMessageEvent ): void
		{
			var info: Object = e.data;
			component_Workspace.setData( info.msg, info.type, info.key );			
		}
		
		override protected function setPosition(): void
		{
			super.setPosition();
			
			logo.y = 3;
			logo.x = 9;
			
			component_StatusBar.y = Align.getPosY( bg, -component_StatusBar.height );
			
			component_List.y = Align.getPosY(  dragArea, 11 );
			//component_List.x = 2;
			
			component_Workspace.y = Align.getPosY( dragArea, 11 );
			component_Workspace.x = bg.width - 9;
			//component_Workspace.x = Align.getPosX(component_List, 1 ) - component_Workspace.width;
			//component_Controller.y = Align.getPosY( component_Workspace, 0 );
			
			
		}
		private function initChild(): void
		{
			this.addChild( bg );
			this.addChild( dragArea );
			this.addChild( btnClose );
			this.addChild( btnMinimize );
			this.addChild( btnPrevSize );
			this.addChild( btnMaximum );
			this.addChild( btnInfo );
			//this.addChild( btnOption );
			this.addChild( component_StatusBar );
			this.addChild( component_List );
			this.addChild( component_Workspace );			
			 
			this.addChild( btnResize );
			
			this.addChild( logo );
		}
		
		private function onResizeingEventListener( e: NativeWindowBoundsEvent ): void
		{
			bg.width 					= stage.stageWidth;
			bg.height 					= stage.stageHeight;
			
			dragArea.width 				= stage.stageWidth;
			
			component_StatusBar.width 	= stage.stageWidth;
			//component_Controller.width 	= stage.stageWidth;
			
			component_Workspace.width = stage.stageWidth - component_List.width - 10;
			component_Workspace.height = stage.stageHeight - dragArea.height - component_StatusBar.height - 8;
			
			component_List.height = stage.stageHeight - dragArea.height - component_StatusBar.height - 8;
			
			this.setPosition();
		}
		
		/**▧▧▧ About SWF LIST ▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧▧**/

		private function onListShowOffEventListener( e: VListEvent ): void
		{
			Tweener.addTween( component_List, { width:8, time:0.3, transition:"easeOutQuart" } );
			
			var wsWidth: int = stage.stageWidth - 18;
			Tweener.addTween( component_Workspace, { width: wsWidth, time:0.3, transition:"easeOutQuart", onUpdate:setPosition } );
		}
		private function onListShowOnEventListener( e: VListEvent ): void
		{
			Tweener.addTween( component_List, { width:int(e.data), time:0.3, transition:"easeOutQuart" } );
			
			//var wsWidth: int = component_Workspace.width - int(e.data) + 8;
			var wsWidth: int = bg.width - int(e.data) - 10;
			Tweener.addTween( component_Workspace, { width: wsWidth, time:0.3, transition:"easeOutQuart", onUpdate:setPosition } );
		}
		private function onListItemClickListener( e: VListItemSelectEvent ): void
		{
			var data: Object = e.data;
			component_Workspace.changeWorkspace( data.key );
			component_StatusBar.txtMsg = data.fileName;
		}
	}
}